package com.quick.db;

import java.util.HashMap;
import com.quick.db.dao.Dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * 简单注入，快速建表，方便查询，多线程安全操作
 * 
 * @author quick
 * 
 */
public class QuickDB extends SQLiteOpenHelper {
	private static final String TAG = QuickDB.class.getSimpleName();
	public static String DB_NAME = "QuickDB.db";
	public static int DB_VERSION = 1;
	private HashMap<Class, Dao> daoMap;
	
	public QuickDB(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	/** 直接执行sql语句 */
	public void exeSQL(String sql) {
		SQLiteDatabase mSQLiteDatabase = getWritableDatabase();
		mSQLiteDatabase.execSQL(sql);
	}

	@SuppressWarnings("unchecked")
	public <T> Dao<T> getDao(Class<T> clazz) {
		Dao<T> dao = null;
		if (daoMap != null) {
			dao = daoMap.get(clazz);
		}else{
			daoMap = new HashMap<Class, Dao>();
		}
		if (dao == null) {
			dao = new Dao<>(clazz, getWritableDatabase());
			daoMap.put(clazz, dao);
		}
		return dao;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

}
